package com.example.list.bigbang;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.list.R;
import com.example.list.recyleview.TV_MSG;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapterCharacter extends RecyclerView.Adapter<MyAdapterCharacter.MyHolder> {

    List<TV_MSG> datas;
    Context context;
    ItemClickListener mItemClickListener ;

    ArrayList<Integer> positinList=new ArrayList<>();

    public MyAdapterCharacter(Context context, List<TV_MSG> datas) {
        this.datas = datas;
        this.context = context;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.character_item2, parent, false);
//        MyHolder holder = new MyHolder(view);
        return new MyHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {

        if (positinList!=null&&positinList.size()>0&&positinList.contains(position)) {
            holder.tv_msg.setText(datas.get(position).getText());
            holder.tv_msg.setBackgroundColor(Color.YELLOW);
        }else {
            holder.tv_msg.setText(datas.get(position).getText());
            holder.tv_msg.setBackgroundColor(datas.get(position).getColor());
        }

//        if (datas.get(position).getColor()== Color.RED) {
//            holder.tv_msg.setText("1");
//            holder.tv_msg.setBackgroundColor(Color.GREEN);
//        }else {
//            holder.tv_msg.setText(datas.get(position).getText());
//            holder.tv_msg.setBackgroundColor(datas.get(position).getColor());
//        }

        // 点击事件一般都写在绑定数据这里，当然写到上边的创建布局时候也是可以的
        if (mItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 这里利用回调来给RecyclerView设置点击事件
                    mItemClickListener.onItemClick(position,v);
                }
            });
        }

        }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {

        TextView tv_msg;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            tv_msg = itemView.findViewById(R.id.tv_msg);
        }
    }

    public interface ItemClickListener{
        void onItemClick(int position, View view) ;
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener){
       mItemClickListener = itemClickListener ;

    }

    public void setPositinList(ArrayList<Integer> indexList) {
        positinList = indexList;
        notifyDataSetChanged();
    }
}
